trace("Testing the EventBroadcaster class");

// Load the class to test:

load("scriptlib/framework/EventBroadcaster.asc");

// Run some tests and report the results:

// Create some listener objects:

listenerA = {name: "listenerA", vote: false};
listenerB = {name: "listenerB", vote: true};
listenerC = {name: "listenerC", vote: null};


function handleEvent1(){
	trace(this.name + ".handleEvent1> arguments.length: " + arguments.length );
	for (var i = 0; i < arguments.length; i++){
		trace("   argument " + i + ": " + arguments[i]);
	}
	return this.vote
}

function handleEvent2(){
	trace(this.name + ".handleEvent2> arguments.length: " + arguments.length );
	for (var i = 0; i < arguments.length; i++){
		trace("   argument " + i + ": " + arguments[i]);
	}
	return this.vote;
}

listenerA.handleEvent1 = handleEvent1;
listenerB.handleEvent1 = handleEvent1;

listenerA.handleEvent2 = handleEvent2;
listenerB.handleEvent2 = handleEvent2;
listenerC.handleEvent2 = handleEvent2;


listenerA.handleEvent1("Test listnet object A ready");
listenerB.handleEvent1("Test listnet object B ready");

// Setup a couple of broadcasters:

broadcasterA = {name: "broadcasterA"};
broadcasterB = {name: "broadcasterB"};

EventBroadcaster.init(broadcasterA);
EventBroadcaster.init(broadcasterB);

// Register some listeners:

broadcasterA.addListener("handleEvent1", listenerA);
broadcasterA.addListener("handleEvent1", listenerB);

// Repeated registrations should only result in one call:
broadcasterA.addListener("handleEvent1", listenerA);
broadcasterA.addListener("handleEvent1", listenerB);

 
// Run the real tests

trace("\n--- Test 1: Two objects receive the same event. ---\n");

broadcasterA.broadcastEvent("handleEvent1", "Welcome to event broadcasting.. expect 2");

// Remove one of the listeners:
trace("\n--- Test 2: One object left to receive the event. ---\n");

broadcasterA.removeListener("handleEvent1", listenerB);
broadcasterA.broadcastEvent("handleEvent1", "Welcome to event broadcasting. expect 1");

// Check return values:

broadcasterB.addListener("handleEvent2", listenerA);
broadcasterB.addListener("handleEvent2", listenerB);
broadcasterB.addListener("handleEvent2", listenerC);

broadcasterB.broadcastEvent("handleEvent1", "Should never see this message because no listeners are registered to receive it..");
broadcasterB.broadcastEvent("handleEvent2", "Should see three of these messages.");

var result = broadcasterB.broadcastEventReturnBoolean("handleEvent2", "Three of these is good. A");
trace("result: false: " + result);

listenerA.vote = null;
var result = broadcasterB.broadcastEventReturnBoolean("handleEvent2", "Three of these is good. B");
trace("result: null: " + result);


listenerA.vote = true;
listenerB.vote = true;
listenerC.vote = true;

var result = broadcasterB.broadcastEventReturnBoolean("handleEvent2", "Three of these is good. C");
trace("result: true: " + result);
